package ar.edu.unq.wannaplay.panels.employee;

import ar.edu.unq.wannaplay.employee.PersonalData;
import ar.edu.unq.wannaplay.exceptions.EmailSyntaxException;
import ar.edu.unq.wannaplay.pages.employee.EditEmployeeLicencesPage;
import ar.edu.unq.wannaplay.pages.employee.EditEmployeeSkillsPage;
import ar.edu.unq.wannaplay.pages.employee.EmployeesViewPage;
import ar.edu.unq.wannaplay.panels.AbstractBasicEditPanel;
import ar.edu.unq.wannaplay.panels.FeedBackPanel;
import ar.edu.unq.wannaplay.services.EmployeeService;
import ar.edu.unq.wannaplay.utils.EmployeeEditer;
import ar.edu.unq.wannaplay.utils.WPLinkCreatedPage;

public class EditEmployeePanel extends AbstractBasicEditPanel<EmployeeEditer, EmployeeService> {

    private static final long serialVersionUID = 1L;

    public EditEmployeePanel(final String id, final EmployeeEditer modelObject, final EmployeeService service,
            final FeedBackPanel feedBackPanel) {
        super(id, modelObject, service, feedBackPanel);

    }

    @Override
    protected String[] getFieldsStrings() {
        return new String[] { "name", "surname", "email", "address", "town", "university" };
    }

    @Override
    protected void addComponents() {
        this.addLinks();
    }

    protected void addLinks() {
        EditEmployeeSkillsPage pageSkills = new EditEmployeeSkillsPage(this.getModelObject(), this.getService());
        WPLinkCreatedPage editSkills = new WPLinkCreatedPage("editEmployeeSkills", pageSkills);
        this.add(editSkills);

        EditEmployeeLicencesPage pageLicences = new EditEmployeeLicencesPage(this.getModelObject(), this.getService());
        WPLinkCreatedPage editLicences = new WPLinkCreatedPage("editEmployeeLicences", pageLicences);
        this.add(editLicences);
    }

    @Override
    protected void acceptAction() {
        try {
            this.getService().updateEmployee(this.preparePersonalData(this.getModelObject()), "",
                    this.getModelObject().getSkillsDTO());
        } catch (EmailSyntaxException e) {
            this.getFeedBackPanel().changeMessage(this.getLocalizer().getString("client.error.emailSyntax", this));
        }
        this.setResponsePage(EmployeesViewPage.class);
    }

    protected PersonalData preparePersonalData(final EmployeeEditer employee) {
        PersonalData personal = new PersonalData();
        personal.setAddress(employee.getAddress());
        personal.setId(employee.getDni());
        personal.setEmail(employee.getEmail());
        personal.setName(employee.getName());
        personal.setSurname(employee.getSurname());
        personal.setTelephone(employee.getTelephone());
        personal.setUniversity(employee.getUniversity());
        personal.setTown(employee.getTown());
        return personal;
    }

}
